package com.rtx.edi.platform.rule.controller;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.model.rule.dto.PlSerialRuleDto;
import com.rtx.edi.platform.rule.bean.PlSerialRule;
import com.rtx.edi.platform.rule.service.PlSerialRuleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = {"流水号规则"})
@RestController
@RequestMapping("/v1/plSerialRule")
public class PlSerialRuleController {
	
	@Autowired
	private PlSerialRuleService plSerialRuleService;
	
	/**
	 * 获取流水号规则列表
	 * @return
	 */
	@ApiOperation(value = "流水号规则列表", notes = "分页获取流水号规则列表")
    @PostMapping("qrySerialRuleList")
	public EdiResponse qrySerialRuleList(@RequestBody PlSerialRuleDto dto) {
		PlSerialRule plSerialRule =new PlSerialRule();
		BeanUtils.copyProperties(dto,plSerialRule);
		Page<PlSerialRule> pageParam = new Page<>(dto.getPageNum(),dto.getPageSize());
		
		plSerialRuleService.selectPlSerialRule(pageParam,plSerialRule);
		
		return EdiResponse.success().data("page",pageParam);
		
	}
	
	@ApiOperation(value = "查询流水号规则", notes = "根据id查询流水号规则")
    @PostMapping("qrySerialRuleById")
	public EdiResponse qrySerialRuleById(@RequestBody PlSerialRuleDto dto) {
		return EdiResponse.success().data("item",plSerialRuleService.getById(dto.getId()));
		
	}
	
	@ApiOperation(value = "新增流水号规则")
    @PostMapping("addSerialRule")
	public EdiResponse addSerialRule(@RequestBody PlSerialRuleDto dto) {
		QueryWrapper<PlSerialRule> qw =new QueryWrapper<PlSerialRule>();
		qw.eq("name", dto.getName());
		qw.eq("code", dto.getCode());
		qw.eq("type", dto.getType());
		boolean exists=plSerialRuleService.getBaseMapper().exists(qw);
		if(exists) {
			return EdiResponse.failed().returnMgr("流水号规则已存在不允许重复创建");
		}
		PlSerialRule plSerialRule =new PlSerialRule();
		BeanUtils.copyProperties(dto,plSerialRule);
	
		return plSerialRuleService.addPlSerialRule(plSerialRule)?EdiResponse.success():EdiResponse.failed();
		
	}
	@ApiOperation(value = "修改流水号规则")
    @PostMapping("updSerialRule")
	public EdiResponse updSerialRule(@RequestBody PlSerialRuleDto dto) {
		PlSerialRule plSerialRule =new PlSerialRule();
		BeanUtils.copyProperties(dto,plSerialRule);
		
		return plSerialRuleService.updateById(plSerialRule)? EdiResponse.success() : EdiResponse.failed();
		
	}
	
	@ApiOperation(value = "批量删除流水号规则")
    @PostMapping("delSerialRuleByIds")
	public EdiResponse delSerialRuleByIds(@RequestBody List<PlSerialRuleDto> list) {
		
		try {
			
			plSerialRuleService.deletePlSerialRule(list);
			return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
		
	}
	
	@ApiOperation(value = "删除流水号规则")
    @PostMapping("delSerialRuleById")
	public EdiResponse delSerialRuleById(@RequestBody PlSerialRuleDto dto) {
		
		
		return plSerialRuleService.removeById(dto.getId())?EdiResponse.success():EdiResponse.failed();
	}

}
